package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import com.itheima.service.SetMealService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Version: V1.0
 */
@RestController
@RequestMapping("/report")
public class ReportController {
    @Reference
    private MemberService memberService;
    @Reference
    private SetMealService setMealService;

    @RequestMapping("/getMemberReport")
    public Result getMemberReport() {
        Map<String, Object> map = new HashMap<String, Object>();

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -24);
        List<String> months = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            Date date = calendar.getTime();
            months.add(new SimpleDateFormat("yyyy.MM").format(date));
            calendar.add(Calendar.MONTH,1);
        }
        map.put("months", months);

        List<Integer> memberCount = memberService.findMemberCountByMonths(months);
        map.put("memberCount", memberCount);

        return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);
    }

    @RequestMapping("/getSetMealReport")
    public Result getSetMealReport(){
        try{
            Map<String,Object> map = new HashMap<>();

            List<Map<String,Object>> setMealCount=setMealService.findSetMealCount();
            map.put("setMealCount",setMealCount);
            List<String> setMealNames = new ArrayList<>();
            for (Map<String, Object> map0 : setMealCount) {
                String name = (String) map0.get("name");
                setMealNames.add(name);
            }
            map.put("setMealNames",setMealNames);

            return new Result(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,map);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
    }
}
